import { screen, imageResource, keyboard, Key, mouse, centerOf, straightTo, Button, linear, clipboard } from '@nut-tree/nut-js';
import '@nut-tree/template-matcher';

// 2、发送消息
export default async function sendMessage(message) {
    // 1.1、点击“消息”按钮
    let region = await screen.find(imageResource('消息-template.png'), { confidence: 0.7 });
    await mouse.move(straightTo(centerOf(region)), linear(500));
    await mouse.click(Button.LEFT);

    // 1.2、打开全局搜索: Ctrl + Alt + F
    await keyboard.pressKey(Key.LeftControl, Key.LeftAlt, Key.F);
    await keyboard.releaseKey(Key.LeftControl, Key.LeftAlt, Key.F);
    
    // 1.3、输入联系人姓名
    // await keyboard.type('技术顽童');
    await keyboard.pressKey(Key.LeftControl, Key.A);
    await keyboard.releaseKey(Key.LeftControl, Key.A);
    await clipboard.setContent('技术顽童');
    await keyboard.pressKey(Key.LeftControl, Key.V);
    await keyboard.releaseKey(Key.LeftControl, Key.V);
    // 1.3、选择联系人Tab
    region = await screen.waitFor(imageResource('联系人Tab-template.png'), 6000, 500, { confidence: 0.7 });
    await mouse.move(straightTo(centerOf(region)), linear(500));
    await mouse.click(Button.LEFT);

    await keyboard.pressKey(Key.Enter);
    await keyboard.releaseKey(Key.Enter);

    // 1.4、输入消息内容
    await screen.waitFor(imageResource('消息发送按钮-disabled-template.png'), 6000, 500, { confidence: 0.7 });
    // await keyboard.type('你好，技术顽童！这是通过nut.js发送的消息。');
    await clipboard.setContent(`你好，技术顽童！这是通过RPA发送的${message || '消息'}。`);
    await keyboard.pressKey(Key.LeftControl, Key.A);
    await keyboard.releaseKey(Key.LeftControl, Key.A);
    await keyboard.pressKey(Key.LeftControl, Key.V);
    await keyboard.releaseKey(Key.LeftControl, Key.V);

    // 1.5、点击“发送”按钮
    region = await screen.waitFor(imageResource('消息发送按钮-template.png'), 6000, 500, { confidence: 0.7 });
    await mouse.move(straightTo(centerOf(region)), linear(500));
    // await mouse.click(Button.LEFT);

}